package gitee.derumin.really_forge.client;

import gitee.derumin.really_forge.block_entities.AnvilBlockEntity;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.RotationAxis;

@Environment(EnvType.CLIENT)
public class AnvilBlockEntityRenderer implements BlockEntityRenderer<AnvilBlockEntity> {

    public AnvilBlockEntityRenderer(BlockEntityRendererFactory.Context ctx){}
    @Override
    public void render(AnvilBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
        this.renderItem(0, matrices, entity, light, overlay, vertexConsumers);
        this.renderItem(1, matrices, entity, light, overlay, vertexConsumers);
    }

    private void renderItem(int index, MatrixStack matrices, AnvilBlockEntity entity, int light, int overlay, VertexConsumerProvider vertexConsumers){
        matrices.push();
        matrices.translate(entity.getXOffset(index),1.016 + index * 0.03125 * (1 - entity.getProgress()), entity.getZOffset(index));
        matrices.multiply(RotationAxis.NEGATIVE_Y.rotationDegrees(entity.getYaw(index)));
        matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(90));
        MinecraftClient.getInstance().getItemRenderer().renderItem(
                entity.getItemStack(index),
                ModelTransformationMode.GROUND,
                light,
                overlay,
                matrices,
                vertexConsumers,
                null,
                0);
        matrices.pop();
    }
}
